/*
    Copyright (C) 2013 maik.jablonski@jease.org

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package jfix.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import jfix.util.Files;

import org.apache.commons.io.IOUtils;

public class Servlets {

	/**
	 * Writes a file with contentType to response.
	 */
	public static void write(File file, String contentType,
			ServletResponse response) {
		try {
			response.setContentType(contentType);
			if (contentType.startsWith("text")) {
				Reader inputReader = Files.newReader(file);
				IOUtils.copy(inputReader, response.getWriter());
				inputReader.close();
			} else {
				response.setContentLength((int) file.length());
				InputStream inputStream = new FileInputStream(file);
				IOUtils.copy(inputStream, response.getOutputStream());
				inputStream.close();
			}
		} catch (IOException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

	/**
	 * Returns the name of the host serving the given request. If a
	 * X-Forwarded-Host-Header is present (e.g. running behind proxy server), it
	 * will be returned, otherwise request.getServerName() (with optional port)
	 * is returned.
	 */
	public static String getHost(HttpServletRequest request) {
		if (request.getHeader("X-Forwarded-Host") != null) {
			return request.getHeader("X-Forwarded-Host");
		} else {
			return request.getServerName()
					+ (request.getServerPort() != 80 ? ":"
							+ request.getServerPort() : "");
		}
	}

	/**
	 * Returns the plain name (without port) of the host serving the given
	 * request. If a X-Forwarded-Host-Header is present (e.g. running behind
	 * proxy server), it will be returned, otherwise request.getServerName() is
	 * returned.
	 */
	public static String getServerName(HttpServletRequest request) {
		if (request.getHeader("X-Forwarded-Host") != null) {
			return request.getHeader("X-Forwarded-Host");
		} else {
			return request.getServerName();
		}
	}

	/**
	 * Returns the url of the host serving the given request.
	 * schema://authority[:port]
	 */
	public static String getServerURL(HttpServletRequest request) {
		return (request.isSecure() ? "https://" : "http://") + getHost(request);
	}

	/**
	 * Returns the server url with context path for the given request.
	 */
	public static String getContextURL(HttpServletRequest request) {
		return (request.isSecure() ? "https://" : "http://") + getHost(request)
				+ request.getContextPath();
	}

}
